﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SmsUI.DataBase;
using System.IO;

namespace SmsUI
{
    public partial class SendedListForm : DevExpress.XtraEditors.XtraUserControl
    {
        smsdbDataSet.tb_SendedListDataTable tb = null;
        public SendedListForm()
        {
            InitializeComponent();
            tb = DbRules.Instance.GetSendedListTable();
          
            this.grdSended.DataSource = tb;
        }



        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DevExpress.XtraEditors.XtraMessageBox.Show("注意，删除条目会影响发送数量和速度计算，确认删除吗？", "删除 ", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation).Equals(DialogResult.OK))
            {
                int[] seleRow = this.gridView1.GetSelectedRows();
                for (int i = seleRow.Length - 1; i > -1; i--)
                {
                    tb[seleRow[i]].Delete();
                }
                DbRules.Instance.UpdateSendedList();
                tb.AcceptChanges();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (DevExpress.XtraEditors.XtraMessageBox.Show("注意，清空条目会影响发送数量和速度计算，确认清空吗？", "清空", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation).Equals(DialogResult.OK))
            {
                for (int i = tb.Rows.Count - 1; i > -1; i--)
                {
                    tb.Rows[i].Delete();
                }

                DbRules.Instance.UpdateSendedList();
                tb.AcceptChanges();
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog savedlg = new SaveFileDialog();
            savedlg.Title = "保存文件";
            savedlg.Filter = "保存文件 (*.xls)|*.xls";
            if (savedlg.ShowDialog() == DialogResult.OK)
            {
                if (cbType.SelectedIndex == 0)
                {
                    this.gridView1.ExportToXls(savedlg.FileName);
                }
                if (cbType.SelectedIndex == 1)
                {
                    DevExpress.XtraGrid.Columns.ColumnFilterInfo info = new DevExpress.XtraGrid.Columns.ColumnFilterInfo("[State] = '成功'");
                    this.gridColumn1.FilterInfo = info;
                    this.gridView1.ExportToXls(savedlg.FileName);
                }
                if (cbType.SelectedIndex == 2)
                {
                    DevExpress.XtraGrid.Columns.ColumnFilterInfo info = new DevExpress.XtraGrid.Columns.ColumnFilterInfo("[State] = '失败'");
                    this.gridColumn1.FilterInfo = info;
                    this.gridView1.ExportToXls(savedlg.FileName);

                }

            }
            DevExpress.XtraGrid.Columns.ColumnFilterInfo info2 = new DevExpress.XtraGrid.Columns.ColumnFilterInfo();

            this.gridColumn1.FilterInfo = info2;
        }

        private void btnExportToTxt_Click(object sender, EventArgs e)
        {

            SaveFileDialog savedlg = new SaveFileDialog();
            savedlg.Title = "保存文本文件";
            savedlg.Filter = "保存文件 (*.txt)|*.txt";
            if (savedlg.ShowDialog() == DialogResult.OK)
            {

                string filename = savedlg.FileName;

                using (FileStream aFile = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(aFile))
                    {
                        foreach (smsdbDataSet.tb_SendedListRow row in tb)
                        {
                            if (cbType.SelectedIndex == 0)
                            {
                                sw.WriteLine(row.PhoneNum);
                            }
                            if (cbType.SelectedIndex == 1)
                            {
                                if (row.State == "成功")
                                {
                                    sw.WriteLine(row.PhoneNum);
                                }
                            }
                            if (cbType.SelectedIndex == 2)
                            {
                                if (row.State == "失败")
                                {
                                    sw.WriteLine(row.PhoneNum);
                                }
                            }
                        }
                    }
                }
            }

        }

        private void btnReSend_Click(object sender, EventArgs e)
        {
            if (DevExpress.XtraEditors.XtraMessageBox.Show("确认重发？", "重发 ", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation).Equals(DialogResult.OK))
            {
                string guid = Guid.NewGuid().ToString();
                using (FileStream aFile = new FileStream(guid, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(aFile))
                    {
                        foreach (smsdbDataSet.tb_SendedListRow row in tb)
                        {
                            if (row.State == "失败")
                            {
                                sw.WriteLine(row.PhoneNum);
                            }
                        }
                    }
                }
                sendListForm sendListForm = ((sendListForm)FormManager.Instance.GetForm("SendListItem"));
                sendListForm.TxtInList(guid);
                File.Delete(guid);
                try
                {
                    for (int i = 0; i < tb.Rows.Count; i++)
                    {
                        if (((smsdbDataSet.tb_SendedListRow)tb.Rows[i]).State == "失败")
                        {
                            tb.Rows[i].Delete();
                        }
                    }
                    DbRules.Instance.UpdateSendedList();
                    tb.AcceptChanges();
                }
                catch
                {

                }


            }
        }
    }
}
